package com.example.websocketdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author HXB
 * @Title:
 * @Package
 * @Description:
 * @date 2020-03-2611:18
 */
@RestController
@RequestMapping("ws")
public class WebSocketController1 {


    SimpMessagingTemplate template;

    @Autowired
    public WebSocketController1(SimpMessagingTemplate template) {
        this.template = template;
    }

    @RequestMapping("test")
    public String test(){
        template.convertAndSend("/topic/subscribe", "您收到了新的系统消息");
        return "hello world";
    }


    @MessageMapping("/hello")
    @SendTo("/topic/hello")
    public String greeting(String message) throws Exception {
        return message;
    }

    @MessageMapping("/message")
    @SendToUser("/message")
    public String userMessage(String userMessage) throws Exception {
        return userMessage;
    }


}
